package com.fmstech.canvasdemo;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        setContentView(new DrawView(this));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class DrawView extends View{

        private Context context;
        DrawView(Context context) {
            super(context);
            this.context = context;
        }

        DrawView(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.context = context;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            //把整张画布绘制成白色
            canvas.drawColor(Color.WHITE);

            /*******初始化Paint********/
            Paint paint = new Paint();
            //去锯齿
            paint.setAntiAlias(true);
            //获取资源文件colors中定义的颜色
            paint.setColor(this.context.getResources().getColor(R.color.common_blue));
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(10.0f);

            /*******开始绘制图形********/
            //绘制圆形
            canvas.drawCircle(150, 150 ,100,paint);

            //绘制正方形
            canvas.drawRect(50.0f ,300.0f,250.0f,500.0f,paint);

            //绘制矩形
            canvas.drawRect(50.0f,550.0f,250.0f, 650.0f, paint);

            //绘制圆角矩形
            RectF rect1 = new RectF(50, 700, 250, 800);
            canvas.drawRoundRect(rect1, 15, 15, paint);

            //绘制椭圆
            RectF oval = new RectF(50, 850, 250, 950);
            canvas.drawOval(oval, paint);

            //定义一个Path对象,封闭成一个三角形
            Path path = new Path();
            path.moveTo(150, 1000);//设置path的起点
            path.lineTo(250, 1200);//第二个点
            path.lineTo(50, 1200);//点三个点
            path.close();
            //根据path进行绘制,绘制三角形
            canvas.drawPath(path, paint);

            //定义一个Path对象,封闭成一个六角形
            Path path1 = new Path();
            path1.moveTo(150, 1250);
            path1.lineTo(250, 1300);
            path1.lineTo(250, 1400);
            path1.lineTo(150, 1450);
            path1.lineTo(50, 1400);
            path1.lineTo(50, 1300);
            path1.close();
            //根据path进行绘制,绘制六角形
            canvas.drawPath(path1, paint);

            /*******设置Paint为填充风格后绘制********/
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.RED);

            //绘制圆形
            canvas.drawCircle(450, 150 ,100,paint);

            //绘制正方形
            canvas.drawRect(350.0f ,300.0f,550.0f,500.0f,paint);

            //绘制矩形
            canvas.drawRect(350.0f,550.0f,550.0f, 650.0f, paint);

            //绘制圆角矩形
            RectF rect2 = new RectF(350, 700, 550, 800);
            canvas.drawRoundRect(rect2, 15, 15, paint);

            //绘制椭圆
            RectF oval1 = new RectF(350, 850, 550, 950);
            canvas.drawOval(oval1, paint);

            //定义一个Path对象,封闭成一个三角形
            Path path2 = new Path();
            path2.moveTo(450, 1000);//设置path的起点
            path2.lineTo(550, 1200);//第二个点
            path2.lineTo(350, 1200);//点三个点
            path2.close();
            //根据path进行绘制,绘制三角形
            canvas.drawPath(path2, paint);

            //定义一个Path对象,封闭成一个六角形
            Path path3 = new Path();
            path3.moveTo(450, 1250);
            path3.lineTo(550, 1300);
            path3.lineTo(550, 1400);
            path3.lineTo(450, 1450);
            path3.lineTo(350, 1400);
            path3.lineTo(350, 1300);
            path3.close();
            //根据path进行绘制,绘制六角形
            canvas.drawPath(path3, paint);

            /*******设置Paint为渐变风格后绘制********/
            //为Paint设置渐变器
            Shader mShader = new LinearGradient(0,0,40,60,
                    new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW},
                    null,Shader.TileMode.REPEAT);
            //为Paint设置渐变器
            paint.setShader(mShader);
            //设置阴影
            paint.setShadowLayer(45, 10, 10, Color.GRAY);

            //绘制圆形
            canvas.drawCircle(750, 150 ,100,paint);

            //绘制正方形
            canvas.drawRect(650.0f ,300.0f,850.0f,500.0f,paint);

            //绘制矩形
            canvas.drawRect(650.0f,550.0f,850.0f, 650.0f, paint);

            //绘制圆角矩形
            RectF rect3 = new RectF(650, 700, 850, 800);
            canvas.drawRoundRect(rect3, 15, 15, paint);

            //绘制椭圆
            RectF oval2 = new RectF(650, 850, 850, 950);
            canvas.drawOval(oval2, paint);

            //定义一个Path对象,封闭成一个三角形
            Path path4 = new Path();
            path4.moveTo(750, 1000);//设置path的起点
            path4.lineTo(850, 1200);//第二个点
            path4.lineTo(650, 1200);//点三个点
            path4.close();
            //根据path进行绘制,绘制三角形
            canvas.drawPath(path4, paint);

            //定义一个Path对象,封闭成一个六角形
            Path path5 = new Path();
            path5.moveTo(750, 1250);
            path5.lineTo(850, 1300);
            path5.lineTo(850, 1400);
            path5.lineTo(750, 1450);
            path5.lineTo(650, 1400);
            path5.lineTo(650, 1300);
            path5.close();
            //根据path进行绘制,绘制六角形
            canvas.drawPath(path5, paint);
        }
    }
}
